<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2022/05/20
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\notice;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;
use ImiApp\ApiServer\Api\Controller\Tpmecms;

/**
 * 通知
 * @Auth(name="token",nologin={"*"})
 * 
 */
class NoticeBean extends Tpmecms
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 改变通知字段
     *
     * @param array $val 
     * @return void
     */
    protected function ChangeFieldData(&$val)
    {
        // $val['comment'] = $this->GetCommentArray($val['id']); //获取通知评论列表
        // $val['user'] = $this->GetUserData2($val['uid']);
        $val['m'] = date('n', $val['createtime']);
        $val['d'] = date('d', $val['createtime']);
        $val['comment_num'] = $this->GetCommentNum($val['id']);
        // $val['self_likes'] = $this->GetUserIsnoticelikes($val['id'], $this->auth->id);
        $val['date_str'] = $this->TimeToStr($val['createtime']);
        return $val;
    }

    /**
     * 获取评论数
     *
     * @param int $typestatus_id app类型id
     * @param int $notice_id id
     * @return int
     */
    protected function GetCommentNum($notice_id)
    {
        return Db::name('notice_comment')->where('notice_id', '=', $notice_id)->where('switch', '=', 1)->count();
    }

    /**
     * 获取评论数组
     *
     * @param int $notice_id id
     * @return void
     */
    protected function GetCommentArray($notice_id)
    {
        // $list = Db::name('notice_comment')->where('notice_id', '=', $notice_id)->where(function ($notice) {
        //     $notice->whereOr('comment_id', '<', 1);
        //     $notice->whereOr('comment_id', 'null');
        // })->orderRaw('time desc')->where('switch', 1)->select()->getArray();

        $list = Db::name('notice_comment')->where('notice_id', '=', $notice_id)->whereRaw('comment_id<1 or comment_id=null')->where('switch','=', 1)->orderRaw('time desc')->select()->getArray();
        foreach ($list as $key => &$val) {
            $val['date'] = $this->TimeToStr($val['time']);
            $val['user'] = $this->GetUserData2($val['uid']);
            $val['comment'] = $this->GetComment2Array($notice_id, $val['id']); // Db::name('notice_comment')->where('notice_id',$notice_id)->where('comment_id',$val['id'])->orderRaw('time desc')->select()->getArray();
        }
        return $list;
    }

    /**
     * 获取评论2维数组
     *
     * @param int $notice_id 获取那个评论
     * @param int $comment_id 评论id
     * @param int $typestatus_id 继承类型
     * @return void
     */
    protected function GetComment2Array($notice_id, $comment_id = null, $typestatus_id = null)
    {
        $list = Db::name('notice_comment')
            ->where('notice_id', '=', $notice_id)
            // ->where('comment_id','>',0)
            ->where('top_id', '=', $comment_id)
            ->where('switch', '=', 1)
            // ->where('top_status','2')
            ->orderRaw('time asc')
            ->select()->getArray();
        foreach ($list as $key => &$val) {
            $val['date'] = $this->TimeToStr($val['time']);
            $val['user'] = $this->GetUserData2($val['uid']);

            if ($val['comment_uid'] == '' || $val['comment_uid'] == null)
                $val['comment_uid'] = Db::name('notice_comment')->where('id', '=', $val['comment_id'])->value('uid');

            $val['nickname'] = Db::name('user')->where('id', '=', $val['comment_uid'])->value('nickname');
            // $val['comment'] = Db::name('notice_comment')->where('notice_id',$notice_id)->where('comment_id','<',$val['id'])->orderRaw('time desc')->select()->getArray();
        }

        // $list = Db::name('notice_comment')->where('notice_id',$notice_id)->where('comment_id',$comment_id)->orderRaw('time desc')->select()->getArray();
        // foreach ($list as $key => &$val) {
        //     $val['date'] = $this->TimeToStr($val['time']);
        //     $val['user'] = $this->GetUserData2($val['uid']);
        //     // $val['comment'] = Db::name('notice_comment')->where('notice_id',$notice_id)->where('comment_id','<',$val['id'])->orderRaw('time desc')->select()->getArray();
        // }

        return $list;
    }

    /**
     * 增加浏览量
     *
     * @param int $id
     * @return void
     */
    protected function AddViews($id)
    {
        Db::name('notice')->where('id', '=', $id)->setFieldInc('views')->update();
        if ($this->auth->isLogin())
            $this->AddViewlog(NOTICE, $id, $this->auth->user()->id);
    }
}
